#system imports
from optparse import OptionParser
import multiprocessing as mp
import os, atexit,trunk
#user imports
from trunk.lib import uplink
from lib import settings, registration, version

pool = []

def cleanup():
    for p in pool:
        p.terminate()

if __name__=="__main__":
    #dealing with options
    parser = OptionParser()
    parser.add_option("-r", "--register", action="callback", callback=registration.register, help="register this node")
    parser.add_option("-v", "--verbose",  action="store_true", dest="verbose", default=False, help="verbose output")
    (options, args) = parser.parse_args()
    #checking server information
    print 'python-strike client v.'+version.trunk()+' is online'
    registration.check()
    key = settings.Key()
    #all checks are done, spawning threads
    print 'node ['+ key.name+ '] is online'
    for number in range(mp.cpu_count()):
        print 'worker #'+str(number)+' spawned'
        p = mp.Process(target=trunk.ignite, args=key.get_args(number))
        pool.append(p)
        p.start()

    atexit.register(cleanup)

    for p in pool:
        p.join()

    print 'terminated'